//
//  MKDateManager.m
//  MKTravel
//
//  Created by song zhang on 2018/9/28.
//  Copyright © 2018年 song zhang. All rights reserved.
//

#import "MBDateManager.h"

@implementation MBDateManager
#pragma mark - 将data类型的时间根据格式转换成时间，有时间差
+(NSString *)dateTransformString:(NSDate *) date style:(NSString *)style
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.locale =  [NSLocale systemLocale];
    formatter.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierISO8601];
    formatter.dateFormat = style;
    NSString *strDate = [formatter stringFromDate:date];
    return strDate;
}
#pragma mark - 根据给定的格式将string类型的时间转换成data类型的时间，有时间差
+(NSDate *)stringTransformDate:(NSString *)strTime style:(NSString *)style
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.locale =  [NSLocale systemLocale];
    formatter.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierISO8601];
    formatter.dateFormat = style;
    return [formatter dateFromString:strTime];
}
 
#pragma mark - 将时间转化为时间戳
+(NSString *) shortStringFromDate:(NSDate*) date
{
    NSTimeInterval a=[date timeIntervalSince1970]*1000;//*1000是精确到毫秒
    NSString *timeSp = [NSString stringWithFormat:@"%0.0f", a];
    return timeSp;
}
#pragma mark - 将时间戳转化为date
+(NSDate *)shortDateFromString:(NSString *)dateStr
{
    if ([SWTool checkEmptyString:dateStr]) {
        return [NSDate date];
    }
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[dateStr doubleValue]/1000];
    return date;
}
#pragma mark - 将时间戳根据格式转换成时间
+(NSString *)shortDateFromString:(NSString *)dateStr style:(NSString *)style
{
    NSDate *date = [self shortDateFromString:dateStr];
    return [self dateTransformString:date style:style];
}
#pragma mark - 时间差
+ (NSInteger)pleaseInsertStarTime:(NSDate *)startDate andInsertEndTime:(NSDate *)endDate
{
    NSTimeInterval time = [endDate timeIntervalSinceDate:startDate];
    NSInteger dayNum = time/(24*60*60);
    return dayNum+1;
}

#pragma mark - 获取当前星期几
+(NSString *)weekdayStringFromDate:(NSDate*)inputDate {
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    [calendar setTimeZone: timeZone];
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
    return [weekdays objectAtIndex:theComponents.weekday];
}


@end
